/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
	CREATES:	${out_tables}/T21A_tobitreg_socioem.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	local child "zptevir_irtscore2 zcog_index zsoc_index" 

	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
	macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	

/*******************************************************************************
Table A21: Tobit regression for the impacts on child socioemotional index at endline
*******************************************************************************/


local thresh "2 5 10 15 20 25 30 40 50 60 75 90"
foreach x of local thresh {
egen p_`x' = pctile(zsoc_index), p(`x')
g zsoc_index_p`x'=.
replace zsoc_index_p`x'=zsoc_index if zsoc_index>=p_`x'
replace zsoc_index_p`x'=p_`x' if zsoc_index<p_`x'
replace zsoc_index_p`x'=p_`x' if attrite==1

}
su p_*

/*
         p_2 |      3,579   -2.170097           0  -2.170097  -2.170097
         p_5 |      3,579   -1.691032           0  -1.691032  -1.691032
        p_10 |      3,579   -1.340023           0  -1.340023  -1.340023
        p_15 |      3,579   -1.089985           0  -1.089985  -1.089985
        p_20 |      3,579   -.8333723           0  -.8333723  -.8333723
-------------+---------------------------------------------------------
        p_25 |      3,579   -.6607254           0  -.6607254  -.6607254
        p_30 |      3,579    -.503787           0   -.503787   -.503787
        p_40 |      3,579   -.1697303           0  -.1697303  -.1697303
        p_50 |      3,579    .0693167           0   .0693167   .0693167
        p_60 |      3,579    .2819961           0   .2819961   .2819961
-------------+---------------------------------------------------------
        p_75 |      3,579     .725408           0    .725408    .725408
        p_90 |      3,579    1.204614           0   1.204614   1.204614


*/

xi: xtreg zsoc_index i.TIPO_old i.sexo_old, fe i(CENTRO_SALUD_old)
estimates store soc_tot
xi: xttobit zsoc_index_p2 i.TIPO_old i.sexo_old, ll(-2.170097) i(CENTRO_SALUD_old)
estimates store soc_p2
xi: xttobit zsoc_index_p5 i.TIPO_old i.sexo_old, ll( -1.691032) i(CENTRO_SALUD_old)
estimates store soc_p5
xi: xttobit zsoc_index_p10 i.TIPO_old i.sexo_old, ll(-1.340023) i(CENTRO_SALUD_old)
estimates store soc_p10
xi: xttobit zsoc_index_p15 i.TIPO_old i.sexo_old, ll(-1.089985 ) i(CENTRO_SALUD_old)
estimates store soc_p15
xi: xttobit zsoc_index_p20 i.TIPO_old i.sexo_old, ll(-.8333723) i(CENTRO_SALUD_old)
estimates store soc_p20
xi: xttobit zsoc_index_p25 i.TIPO_old i.sexo_old, ll(-.6607254) i(CENTRO_SALUD_old)
estimates store soc_p25
xi: xttobit zsoc_index_p30 i.TIPO_old i.sexo_old, ll( -.503787 ) i(CENTRO_SALUD_old)
estimates store soc_p30
xi: xttobit zsoc_index_p40 i.TIPO_old i.sexo_old, ll(-.1697303   ) i(CENTRO_SALUD_old)
estimates store soc_p40
xi: xttobit zsoc_index_p50 i.TIPO_old i.sexo_old, ll(.0693167 ) i(CENTRO_SALUD_old)
estimates store soc_p50
xi: xttobit zsoc_index_p60 i.TIPO_old i.sexo_old, ll(.2819961) i(CENTRO_SALUD_old)
estimates store soc_p60
xi: xttobit zsoc_index_p75 i.TIPO_old i.sexo_old, ll(.725408) i(CENTRO_SALUD_old)
estimates store soc_p75
xi: xttobit zsoc_index_p90 i.TIPO_old i.sexo_old, ll( 1.204614) i(CENTRO_SALUD_old)
estimates store soc_p90



esttab soc_tot soc_p5 soc_p10 soc_p20 soc_p30 soc_p40 soc_p50 /*
*/ using "$out_tables/appendix/T21A_tobitreg_socioem.csv", replace f /*
*/ keep(_ITIPO_old_2 _ITIPO_old_3) /*
*/ cells("b(fmt(3)star)" "se(fmt(3)par)") /*
*/ star(* 0.10 ** 0.05 *** 0.01) /*
*/ stats(r2 N, fmt(2 0) labels("R-squared" "Observations" )) legend	/*
*/ mtitles("OLS" "TOBIT 5%" "TOBIT 10%" "TOBIT 20%" "TOBIT 30%" "TOBIT 40%" "TOBIT 50%"  ) /*
*/ varlabels(_ITIPO_old_2 "NEP-B" _ITIPO_old_3 "NEP-I")
		
		
		
		